package com.iplant.aps.serviceimpl.dao.mes;

import com.iplant.aps.service.mesenum.MESDBSource;
import com.iplant.aps.service.mesenum.MESException;
import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.mes.MESSystemConfig;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置数据访问类
 *
 * @author YouWang·Peng
 * 2023-12-12 15:22:53
 */
public class MESSystemConfigDAO extends BaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(MESSystemConfigDAO.class);

    //region 单实例
    private static MESSystemConfigDAO Instance = null;

    private MESSystemConfigDAO() {
        super();
    }

    public static MESSystemConfigDAO getInstance() {
        if (Instance == null)
            Instance = new MESSystemConfigDAO();
        return Instance;
    }
    //endregion

    /**
     * 查单条
     */
    public MESSystemConfig SelectByKey(BMSEmployee wLoginUser, String wKey, OutResult<Integer> wErrorCode) {
        MESSystemConfig wResult = new MESSystemConfig();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            List<MESSystemConfig> wList = SelectList(wLoginUser, wKey, "", wErrorCode);
            if (wList == null || wList.size() != 1)
                return wResult;
            wResult = wList.get(0);
        } catch (Exception e) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 查单条
     */
    public String SelectValueByKey(BMSEmployee wLoginUser, String wKey, OutResult<Integer> wErrorCode) {
        String wResult = "";
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            List<MESSystemConfig> wList = SelectList(wLoginUser, wKey, "", wErrorCode);
            if (wList == null || wList.size() != 1)
                return wResult;
            wResult = wList.get(0).Value;
        } catch (Exception e) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 条件查询集合
     */
    public List<MESSystemConfig> SelectList(BMSEmployee wLoginUser, String wKey, String wValue,
                                            OutResult<Integer> wErrorCode) {
        List<MESSystemConfig> wResultList = new ArrayList<MESSystemConfig>();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResultList;
            }

            String wSQL = MessageFormat.format("SELECT * FROM {0}.mes_systemconfig WHERE  1=1  "
                            + "and ( :wKey = '''' or :wKey = `Key` ) " + "and ( :wValue = '''' or :wValue = `Value` );",
                    wInstance.Result);

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wParamMap.put("wKey", wKey);
            wParamMap.put("wValue", wValue);

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = shrisJdbcTemplate.queryForList(wSQL, wParamMap);

            for (Map<String, Object> wReader : wQueryResult) {
                MESSystemConfig wItem = new MESSystemConfig();

                wItem.Key = StringUtils.parseString(wReader.get("Key"));
                wItem.Value = StringUtils.parseString(wReader.get("Value"));

                wResultList.add(wItem);
            }
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResultList;
    }


}
